/**
 * 数据传输对象(DTO)类型定义
 * 统一前后端数据结构，解决类型不匹配问题
 */

// 基础响应接口
export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  message?: string;
  code?: string;
  timestamp: string;
}

// 分页响应接口
export interface PaginatedResponse<T> extends ApiResponse<T[]> {
  pagination: {
    current: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

// 产品相关DTO
export interface ProductDTO {
  id: string;
  name: string;
  description?: string;
  type: string;
  categoryId: string; // 映射到type字段，兼容前端
  priceRange?: {
    min: number;
    max: number;
  };
  unit?: string;
  images: string[];
  features?: string[];
  materials?: string[];
  craftTypes?: CraftTypeDTO[];
  specifications?: any;
  featured: boolean;
  status: 'active' | 'inactive' | 'draft';
  sort: number;
  createdAt: string;
  updatedAt: string;
}

export interface CreateProductDTO {
  name: string;
  description?: string;
  type: string;
  categoryId?: string;
  priceMin?: number;
  priceMax?: number;
  unit?: string;
  images?: string[];
  features?: string[];
  materials?: string[];
  craftTypes?: any[];
  specifications?: any;
  featured?: boolean;
  status?: 'active' | 'inactive' | 'draft';
  sort?: number;
}

export interface UpdateProductDTO extends Partial<CreateProductDTO> {
  id: string;
}

// 工艺类型DTO
export interface CraftTypeDTO {
  id: string;
  productId: string;
  name: string;
  description?: string;
  sortOrder: number;
  status: 'active' | 'inactive';
  createdAt: string;
  updatedAt: string;
  images?: ProductImageDTO[];
  specifications?: ProductSpecificationDTO[];
  priceConfigurations?: PriceConfigurationDTO[];
}

// 产品图片DTO
export interface ProductImageDTO {
  id: string;
  productId?: string;
  craftTypeId?: string;
  imageUrl: string;
  altText?: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

// 产品规格DTO
export interface ProductSpecificationDTO {
  id: string;
  productId?: string;
  craftTypeId?: string;
  name: string;
  value: string;
  unit?: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

// 价格配置DTO
export interface PriceConfigurationDTO {
  id: string;
  productId?: string;
  craftTypeId?: string;
  name: string;
  basePrice: number;
  minQuantity?: number;
  maxQuantity?: number;
  unit?: string;
  description?: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
}

// 案例相关DTO
export interface CaseDTO {
  id: string;
  title: string;
  description?: string;
  industry: string;
  category: string; // 映射到industry字段，兼容前端
  client: string; // 映射到clientName字段
  clientName: string;
  duration?: string;
  completedAt?: string;
  projectDate?: string; // 映射到completedAt字段
  location?: string;
  content?: string;
  images: string[];
  services?: string[];
  tags: string[];
  challenges?: string;
  solutions?: string;
  results?: string;
  technologies?: any[];
  highlights?: string[];
  background?: string;
  requirements?: string;
  projectValue?: string;
  featured: boolean;
  status: 'active' | 'inactive' | 'draft';
  sort: number;
  createdAt: string;
  updatedAt: string;
}

export interface CreateCaseDTO {
  title: string;
  description?: string;
  industry: string;
  category?: string;
  clientName?: string;
  duration?: string;
  completedAt?: string;
  projectDate?: string;
  location?: string;
  content?: string;
  images?: string[];
  services?: string[];
  tags?: string[];
  challenges?: string;
  solutions?: string;
  results?: string;
  technologies?: any[];
  highlights?: string[];
  background?: string;
  requirements?: string;
  projectValue?: string;
  featured?: boolean;
  status?: 'active' | 'inactive' | 'draft';
  sort?: number;
}

export interface UpdateCaseDTO extends Partial<CreateCaseDTO> {
  id: string;
}

// 查询参数DTO
export interface ProductQueryDTO {
  page?: number;
  pageSize?: number;
  type?: string;
  status?: string;
  featured?: boolean;
  keyword?: string;
  priceRange?: {
    min?: number;
    max?: number;
  };
  sortBy?: 'createdAt' | 'updatedAt' | 'sort' | 'name';
  sortOrder?: 'asc' | 'desc';
}

export interface CaseQueryDTO {
  page?: number;
  pageSize?: number;
  industry?: string;
  category?: string;
  status?: string;
  featured?: boolean;
  keyword?: string;
  tags?: string[];
  sortBy?: 'createdAt' | 'updatedAt' | 'sort' | 'title' | 'completedAt';
  sortOrder?: 'asc' | 'desc';
}

// 标签DTO
export interface TagDTO {
  id: string;
  name: string;
  type: 'case' | 'product';
  color?: string;
  usageCount: number;
  createdAt: string;
}

// 文件上传DTO
export interface UploadDTO {
  id: string;
  filename: string;
  originalName: string;
  mimeType: string;
  size: number;
  path: string;
  url: string;
  uploadedBy?: string;
  category: 'product' | 'case' | 'avatar' | 'document' | 'general';
  status: 'active' | 'deleted';
  createdAt: string;
}

// 轮播图DTO
export interface BannerDTO {
  id: string;
  title: string;
  subtitle?: string;
  image: string;
  sort: number;
  status: 'active' | 'inactive';
  createdAt: string;
  updatedAt: string;
}

// 系统设置DTO
export interface SettingDTO {
  key: string;
  value: string;
  description: string;
  type: 'string' | 'number' | 'boolean' | 'json';
  createdAt: string;
  updatedAt: string;
}

// 用户DTO
export interface UserDTO {
  id: string;
  username: string;
  email: string;
  name: string;
  role: 'user' | 'admin';
  avatar?: string;
  status: 'active' | 'inactive';
  lastLoginAt?: string;
  createdAt: string;
  updatedAt: string;
}

// 认证相关DTO
export interface LoginDTO {
  username: string;
  password: string;
}

export interface AuthResponseDTO {
  user: UserDTO;
  token: string;
  expiresAt: string;
}

// 统计数据DTO
export interface DashboardStatsDTO {
  totalProducts: number;
  totalCases: number;
  totalUsers: number;
  recentActivity: {
    products: number;
    cases: number;
    uploads: number;
  };
}

// 数据映射工具类型
export interface DataMapper<TEntity, TDTO> {
  toDTO(entity: TEntity): TDTO;
  fromDTO(dto: TDTO): Partial<TEntity>;
}

// 常用状态枚举
export enum EntityStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  DRAFT = 'draft'
}

export enum UserRole {
  USER = 'user',
  ADMIN = 'admin'
}

export enum UploadCategory {
  PRODUCT = 'product',
  CASE = 'case',
  AVATAR = 'avatar',
  DOCUMENT = 'document',
  GENERAL = 'general'
}

export enum TagType {
  CASE = 'case',
  PRODUCT = 'product'
}